package com.acme.bankapp.domain.bank;

import java.util.ArrayList;
import java.util.List;

import com.acme.bankapp.domain.bank.Account;

public class Client {
	private Gender gender;
	private String name;
	private List<Account> accounts;

	public Client(Gender gender, String name, List<Account> accounts) {
		this(gender, name);
		for (Account account : accounts) {
			this.addAccount(account);
		}
	}

	public Client(Gender gender, String name, Account account) {
		this(gender, name);
		this.addAccount(account);
	}
	public Client(Gender gender, String name) {
		this.gender = gender;
		this.name = name;
		this.accounts = new ArrayList<Account>();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (!gender.toString().equals(other.gender.toString()))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public String getClientSalutation() {
		return this.gender.toString() + this.name;
	}

	public void addAccount(Account account) {
		accounts.add(account);
		account.setClient(this);
	}

	public List<Account> getAccounts() {
		return accounts;
	}
	
	@Override
	public String toString() {
		StringBuffer clientString = new StringBuffer(this.getClientSalutation());
		for (Account acc: getAccounts()) {
			clientString.append(acc.toString());
		}
		return clientString.toString();
		
	}

}
